package com.lin.services.impl;

import com.lin.dao.OrderDao;
import com.lin.dao.PriceChangeDao;
import com.lin.dao.WaresDao;
import com.lin.pojo.Wares;
import com.lin.pojo.Order;
import com.lin.pojo.WaresOrder;
import com.lin.services.ReduceOrderService;
import com.lin.utils.ShiroUtil;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName ReduceOrderServiceImpl
 * Package com.lin.services.impl
 *
 * @author hellcat
 * Date 2020/3/17
 * Description:
 * 🐾
 */
@Service
public class ReduceOrderServiceImpl implements ReduceOrderService {
    @Autowired
    private WaresDao waresDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public List<Order> getReduceOrder(String searchDate, String searchOrderId) {
        try {
            String[] split = searchDate.split(" - ");
            return orderDao.getOrder(split[0], split[1], searchOrderId, ShiroUtil.getUser().getStoreId(), 0);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<Wares> getAllWares(String id) {
        try {
            return waresDao.getWarsList(id, "", "", "", ShiroUtil.getUser().getStoreId());
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Map<String, Object> insertReduceOrder(List<WaresOrder> tableData) {
        Map<String, Object> map = new HashMap<>();
        try {
            Order order = new Order();
            order.setOrderId("TH" + System.currentTimeMillis() + RandomUtils.nextInt(10) + "" + RandomUtils.nextInt(10));
            order.setStoreId(ShiroUtil.getUser().getStoreId());
            order.setCreator(ShiroUtil.getUser().getName());
            order.setType(0);
            order.setWaresNum(tableData.size());
            double amount = 0.0;
            for (WaresOrder tableDatum : tableData) {
                amount = amount + tableDatum.getOrderNumber() * tableDatum.getUnitPrice();

            }
            order.setAmount(amount);
            orderDao.insertOrder(order);
            for (WaresOrder tableDatum : tableData) {
                WaresOrder waresOrder = new WaresOrder();
                waresOrder.setOrderId(order.getId());
                waresOrder.setProductionDate(tableDatum.getProductionDate());
                waresOrder.setWaresId(tableDatum.getWaresId());
                waresOrder.setUnitPrice(tableDatum.getUnitPrice());
                waresOrder.setOrderNumber(tableDatum.getOrderNumber());
                orderDao.insertWaresOrder(waresOrder);
                //更新wares相关数据 库存
                Wares wares = waresDao.findWaresById(tableDatum.getWaresId(), ShiroUtil.getUser().getStoreId());
                int newInventory = wares.getInventory() - tableDatum.getOrderNumber();
                wares.setInventory(newInventory);
                waresDao.updateWare(wares);
            }
            map.put("code", true);
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "创建失败");
//            手动执行事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }

    @Override
    public Order getOrderById(String id) {
        return orderDao.getOrderById(id, ShiroUtil.getUser().getStoreId());
    }

    @Override
    public List<WaresOrder> getOrderWaresByOrderId(String id) {
        try {
            return orderDao.getOrderWaresByOrderId(id, ShiroUtil.getUser().getStoreId());
        } catch (Exception e) {
            return null;
        }
    }
}
